/**
 *******************************************************************************
 * @file  usb/usb_dev_cdc/source/cdc_data_process.c
 * @brief Generic media access Layer.
 @verbatim
   Change Logs:
   Date             Author          Notes
   2020-06-12       CDT             First version
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by HDSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 *
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "cdc_data_process.h"
#include "usb_app_conf.h"

#include <bl/support.h>

/**
 * @addtogroup HC32F4A0_DDL_Examples
 * @{
 */

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/
LINE_CODING linecoding =
  {
    115200, // baud rate - 115200
    0x00,   // stop bits - 1
    0x00,   // parity    - none
    0x08    // data bit  - 8
  };
/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
extern uint8_t  uart_rx_buffer[APP_RX_DATA_SIZE]; 
extern volatile uint32_t APP_Rx_ptr_in;   
extern usb_core_instance usb_dev;

/**
 *******************************************************************************
 ** \brief  Initializes the configuration of usart port
 ** \param  none
 ** \retval none
 ******************************************************************************/
void vcp_init(void)
{
    
}

/**
 *******************************************************************************
 ** \brief  deInitializes the Media
 ** \param  none
 ** \retval none
 ******************************************************************************/
void vcp_deinit(void)
{
   
}

/**
 *******************************************************************************
 ** \brief  Manage the CDC class requests
 ** \param  Cmd: Command code
 ** \param  Buf: data to be sent or received
 ** \param  Len: data length in bytes
 ** \retval status
 ******************************************************************************/
void vcp_ctrlpare (uint32_t Cmd, uint8_t* Buf, uint32_t Len)
{
    switch (Cmd)
    {
        case SEND_ENCAPSULATED_COMMAND:
        /* Not  needed for this driver */
        break;

        case GET_ENCAPSULATED_RESPONSE:
        /* Not  needed for this driver */
        break;

        case SET_COMM_FEATURE:
        /* Not  needed for this driver */
        break;

        case GET_COMM_FEATURE:
        /* Not  needed for this driver */
        break;

        case CLEAR_COMM_FEATURE:
        /* Not  needed for this driver */
        break;

        case SET_LINE_CODING:
            linecoding.bitrate = ((uint32_t)Buf[0U] | ((uint32_t)Buf[1U] << 8U) | ((uint32_t)Buf[2U] << 16U) | ((uint32_t)Buf[3U] << 24U));
            linecoding.format = Buf[4];
            linecoding.paritytype = Buf[5U];
            linecoding.datatype = Buf[6U];
        break;
        case GET_LINE_CODING:
            Buf[0U] = (uint8_t)(linecoding.bitrate);
            Buf[1U] = (uint8_t)(linecoding.bitrate >> 8U);
            Buf[2U] = (uint8_t)(linecoding.bitrate >> 16U);
            Buf[3U] = (uint8_t)(linecoding.bitrate >> 24U);
            Buf[4U] = linecoding.format;
            Buf[5U] = linecoding.paritytype;
            Buf[6U] = linecoding.datatype;
        break;
        case SET_CONTROL_LINE_STATE:
        /* Not  needed for this driver */
        break;
        case SEND_BREAK:
        /* Not  needed for this driver */
        break;
        default:
        break;
    }
}

extern void buf_put(uint8_t b);
extern void process_asynchdata_uart2usb(void *pdev);
extern int buf_get(void);

/**
 *******************************************************************************
 ** \brief  Data received from USB are sent to uart port
 ** \param  Buf: buffer to be sent to uart port 
 ** \param  Len: data length in bytes
 ** \retval none
 ******************************************************************************/
void vcp_rxdata (uint8_t* Buf, uint32_t Len)
{
    for (uint32_t i=0; i<Len; i++) {
        buf_put(Buf[i]);
    }
}

void usb_cout(uint8_t *buf, unsigned len)
{
    for (unsigned i=0; i<len; i++) {
        uart_rx_buffer[APP_Rx_ptr_in++] = buf[i];

        if(APP_Rx_ptr_in == APP_RX_DATA_SIZE)
        {
            APP_Rx_ptr_in = 0U;
        }
    }

#if !defined(CDC_IN_FRAME_INTERVAL)
    process_asynchdata_uart2usb(&usb_dev);
#endif

}

int usb_cin(void)
{
    return buf_get();
}
/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
